package com.office.automate.servlet.frameworks;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.configuration.property.DatagridPageConfiguration;
import com.office.automate.dto.convertors.AdministrationDetailConvertor;
import com.office.automate.dto.convertors.EmployeeDetailsConvertor;
import com.office.automate.dto.convertors.LeaveDetailsConvertor;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.frontier.utility.DataConversionUtility;
import com.office.automate.model.administration.Branch;
import com.office.automate.model.administration.Division;
import com.office.automate.model.administration.SecurityLevel;
import com.office.automate.model.employee.Employee;
import com.office.automate.model.employee.EmployeeQualification;
import com.office.automate.model.leave.LeaveRequest;
import com.office.automate.service.def.administration.AdministrationService;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.service.def.leave.EmployeeLeaveService;
import com.office.automate.service.impl.administration.AdministrationServiceImplementer;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;
import com.office.automate.service.impl.leave.EmployeeLeaveServiceImplementer;

/**
 * Servlet implementation class DataGridComponentServlet
 */
@WebServlet("/DataGridComponentDeleteActionServlet")
public class DataGridComponentDeleteActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DataGridComponentDeleteActionServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = null;
		String datagridPageID = (String) request.getParameter("DataGridPageID");
		try {
			if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.SECURITY_LEVEL)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageSecurityLevels.jsp");
				AdministrationService administrationService = new AdministrationServiceImplementer();
				SecurityLevel securityLevel = new AdministrationDetailConvertor().convertSecurityLevelDataGridRequest(request);
				administrationService.deleteSecurityLevel(securityLevel);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.BRANCH_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageBranches.jsp");
				AdministrationService administrationService = new AdministrationServiceImplementer();
				Branch branch = new AdministrationDetailConvertor().convertBranchDataGridRequest(request);
				administrationService.deleteBranch(branch);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.DIVISION_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageDivisions.jsp");
				AdministrationService administrationService = new AdministrationServiceImplementer();
				Division division = new AdministrationDetailConvertor().convertDivisionDataGridRequest(request);
				administrationService.deleteDivision(division);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_QUALIFICATION_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/Qualifications.jsp");
				EmployeeService employeeService = new EmployeeServiceImplementer();
				EmployeeQualification employeeQualification = new EmployeeDetailsConvertor().convertQualificationDataGridRequest(request);
				employeeService.deleteQualification(employeeQualification);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/Search.jsp");
				EmployeeService employeeService = new EmployeeServiceImplementer();
				Employee employee = new EmployeeDetailsConvertor().convertEmployeeGridRequest(request);
				employeeService.deleteEmployee(employee);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_LEAVE_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApplyForLeave.jsp");
				EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();
				LeaveRequest leaveRequest = new LeaveDetailsConvertor().convertEmployeeGridRequest(request);
				employeeLeaveService.cancelLeaveRequest(leaveRequest);
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_LEAVE_APPROVE)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApproveLeave.jsp");
				EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();
				employeeLeaveService.rejectLeaveRequest(new DataConversionUtility().convertToLong(request.getParameter("Field5")));
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.PUBLIC_HOLIDAYS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/AddPublicHolidays.jsp");
				AdministrationService administrationService = new AdministrationServiceImplementer();
				administrationService.deletePublicHoliday(new DataConversionUtility().convertToLong(request.getParameter("Field3")));
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.APPROVE_LIEU_LEAVE)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApproveLieuLeave.jsp");
				EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();
				employeeLeaveService.rejectLieuLeaveRequest(new DataConversionUtility().convertToLong(request.getParameter("Field3")));
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.APPROVE_OVER_TIME)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ApproveOverTimeRequest.jsp");
				EmployeeService employeeService = new EmployeeServiceImplementer();
				employeeService.rejectOverTimeRequest(new DataConversionUtility().convertToLong(request.getParameter("Field4")));
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.LIEU_LEAVE_REQUEST)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/RequestForLieuLeave.jsp");
				EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();
				employeeLeaveService.cancelLieuLeaveRequest(new DataConversionUtility().convertToLong(request.getParameter("Field4")));
				dispatcher.forward(request, response);
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.OVER_TIME_REQUEST)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ApplyForOverTime.jsp");
				EmployeeService employeeService = new EmployeeServiceImplementer();
				employeeService.cancelOverTimeRequest(new DataConversionUtility().convertToLong(request.getParameter("Field5")));
				dispatcher.forward(request, response);
			}
		} catch (FrontierVisibleException e) {
			request.setAttribute("OperationalError", e);
			dispatcher.forward(request, response);	
		}
	}
}
